<?php
class ExceptionscasesController extends AppController {

	var $name = 'Exceptionscases';

	function beforeRender() {
		$groupid = $this->Auth->user('group_id');
		$name = 'Exceptionscases';
		IF($groupid != 1){
			$result1 = mysql_query("SELECT id FROM applications WHERE name = '$name'");
			$appid = mysql_fetch_object($result1)->id;
			$result2 = mysql_query("SELECT count(*) as count FROM groupshasrights WHERE group_id = '$groupid' AND application_id = '$appid'");
			$count = mysql_fetch_object($result2)->count;
			IF($count == 0){
				$this->Session->setFlash(__('Access Denied', true));
				$this->redirect(array('controller' => 'pages' ,'action' => 'error'));	
			}
		}
	}
	
	function index() {
		$this->Exceptionscase->recursive = 0;
		$this->set('exceptionscases', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid exceptionscase', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('exceptionscase', $this->Exceptionscase->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Exceptionscase->create();
			if ($this->Exceptionscase->save($this->data)) {
				$this->Session->setFlash(__('The exceptionscase has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The exceptionscase could not be saved. Please, try again.', true));
			}
		}
		$statuses = $this->Exceptionscase->Status->find('list');
		$weborders = $this->Exceptionscase->Weborder->find('list');
		$this->set(compact('statuses', 'weborders'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid exceptionscase', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Exceptionscase->save($this->data)) {
				$this->Session->setFlash(__('The exceptionscase has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The exceptionscase could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Exceptionscase->read(null, $id);
		}
		$statuses = $this->Exceptionscase->Status->find('list');
		$weborders = $this->Exceptionscase->Weborder->find('list');
		$this->set(compact('statuses', 'weborders'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for exceptionscase', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Exceptionscase->delete($id)) {
			$this->Session->setFlash(__('Exceptionscase deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Exceptionscase was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
?>